﻿/************************************************************************

   Extended Silverlight Toolkit

   Copyright (C) 2010-2012 Xceed Software Inc.

   This program is provided to you under the terms of the Microsoft Public
   License (Ms-PL) as published at http://extendedsilverlight.codeplex.com/license

   Please purchase a commercial version of this toolkit if you are using
   it in a commercial product. The commercial versions support the project,
   and also include more features for each control, priority support,
   source code, updates, removed watermark, and a proprietary license.

   Visit http://xceed.com and follow @datagrid on Twitter.

  **********************************************************************/

using System;

namespace Xceed.Silverlight.Data
{
  internal abstract class MoveConverterEnumerator<T> : FetchPagingEnumerator<T>
  {
    #region Static Fields

    private static readonly Guid JumpMoveAndFetchCallId = DataUtils.CreateCallId();

    #endregion

    #region Constructor

    internal MoveConverterEnumerator( IAsyncEnumerator childEnumerator )
      : base( childEnumerator )
    {
    }

    #endregion

    #region (Begin/End)JumpMoveAndFetch Methods

    public override JumpMoveAndFetchAsyncResult BeginJumpMoveAndFetch( JumpMoveAndFetchParameters parameters, AsyncCallback userCallback, object asyncState )
    {
      JumpMoveAndFetchAsyncResult asyncResult = new JumpMoveAndFetchAsyncResult( MoveConverterEnumerator<T>.JumpMoveAndFetchCallId, userCallback, asyncState, parameters );

      if( this.ConvertMoveForRequest( parameters ) )
      {
        asyncResult.Start(
          ( cb, st ) => this.BaseBeginJumpMoveAndFetch( parameters, cb, st ),
          ( par, iar ) => new DependentCallResult( this.BaseEndJumpMoveAndFetch( iar ) ) );
      }
      else
      {
        asyncResult.Start(
          ( cb, st ) => this.ChildEnumeratorTyped.BeginJumpMoveAndFetch( parameters, cb, st ),
          ( par, iar ) => new DependentCallResult( this.ChildEnumeratorTyped.EndJumpMoveAndFetch( ( JumpMoveAndFetchAsyncResult )iar ) ) );
      }

      return asyncResult;
    }

    public override JumpMoveAndFetchResult<T> EndJumpMoveAndFetch( JumpMoveAndFetchAsyncResult asyncResult )
    {
      asyncResult.ValidateCallEnd( MoveConverterEnumerator<T>.JumpMoveAndFetchCallId );

      return asyncResult.GetCallResult<JumpMoveAndFetchResult<T>>();
    }

    #endregion

    #region Base(Begin/End)JumpMoveAndFetch Methods

    private JumpMoveAndFetchAsyncResult BaseBeginJumpMoveAndFetch( JumpMoveAndFetchParameters parameters, AsyncCallback userCallback, object asyncState )
    {
      return base.BeginJumpMoveAndFetch( parameters, userCallback, asyncState );
    }

    private JumpMoveAndFetchResult<T> BaseEndJumpMoveAndFetch( IAsyncResult asyncResult )
    {
      return base.EndJumpMoveAndFetch( ( JumpMoveAndFetchAsyncResult )asyncResult );
    }

    #endregion

    protected abstract bool ConvertMoveForRequest( JumpMoveAndFetchParameters parameters );
  }
}
